import {Component, OnInit} from '@angular/core';
import { NzMessageService} from 'ng-zorro-antd';
import {UtilService} from "../../my-service/util.service";
import {Router, ActivatedRoute} from "@angular/router";
import {TrainingService} from "../../my-service/training.service";

declare let io: any;

@Component({
  selector: 'app-result',
  templateUrl: './result.component.html',
  styleUrls: ['./result.component.css'],
})
export class ResultComponent implements OnInit {
  trainingID = '';
  answerCardID = '';
  userID = '';
  score: number;
  coverUrl = '';//封面图片
  socket: any;

  constructor(private message: NzMessageService,
              private utilServe: UtilService,
              private router: Router,
              private routerInfo: ActivatedRoute,
              private trainingService: TrainingService) {
    /*创建socket连接*/
    this.socket = io.connect('wws://creativityio.lessonplan.cn');
  }

  getAnswerCard(answerID) {
    this.trainingService.getAnswerCardById(answerID).then(res => {
      if (res.status == 1) {
        this.score = parseInt(res.data[0].Score); //获取成绩
        this.countCreate(this.score);
      }
    })
  }

  //计算创造性特征
  countCreate(score) {
    if (score >= 95) {
      //创造行非凡
      this.coverUrl = '/assets/img/image/result1.jpg'
    } else if (score >= 65) {
      //很有创造性
      this.coverUrl = '/assets/img/image/result2.jpg'
    } else if (score >= 40) {
      //高于平均水平
      this.coverUrl = '/assets/img/image/result3.jpg'
    } else if (score >= 20) {
      //平均水平
      this.coverUrl = '/assets/img/image/result4.jpg'
    } else if (score >= 10) {
      //低于平均水平
      this.coverUrl = '/assets/img/image/result5.jpg'
    } else {
      //无创造性
      this.coverUrl = '/assets/img/image/result6.jpg'
    }
  }

  ngOnInit() {
    this.answerCardID = this.routerInfo.snapshot.params.cardID;
    this.trainingID = this.routerInfo.snapshot.params.id;
    this.socket.emit('success', {answerCardID: this.answerCardID, trainingID: this.trainingID});
    this.getAnswerCard(this.answerCardID);
  }
}
